package m202405.d11;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/11 13:24
 */
public class Q15 {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int target = -nums[i];
            for (int left = i + 1, right = nums.length - 1; left < nums.length; left++) {
                if (left>i+1 && nums[left] == nums[left - 1]) {
                    continue;
                }
                while (left < right && nums[left] + nums[right] > target) {
                    right--;
                }
                if (left == right) {
                    break;
                }
                if (nums[left] + nums[right] == target) {
                    res.add(List.of(nums[i], nums[left], nums[right]));
                }
            }
        }
        return res;
    }
}
